package com.baizhi.controller;

import com.baizhi.service.UserDTOService;
import com.baizhi.utils.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Controller
public class UserDTOController {
    @Autowired
    private UserDTOService userDTOService;

    @RequestMapping("/selectAllUserDTO")
    @ResponseBody
    public Map selectAll(){
        Map map=new HashMap();
        List<Map> sexCount = userDTOService.selectSexCount();
        List<Map> provinceCount = userDTOService.selectProvinceCount();
        int[] ids = userDTOService.selectDateCount();

        map.put("sexCount",sexCount);
        map.put("provinceCount",provinceCount);
        map.put("ids",ids);

        return  map;
    }

    @RequestMapping("/selectAllUserDTO2")
    @ResponseBody
    public Map selectAll2() throws InterruptedException {
      final  Map map=new HashMap();
      final CountDownLatch countDownLatch=new CountDownLatch(3);
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                int[] ids = userDTOService.selectDateCount();
                map.put("ids",ids);
                countDownLatch.countDown();
            }
        });

        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> provinceCount = userDTOService.selectProvinceCount();
                map.put("provinceCount",provinceCount);
                countDownLatch.countDown();
            }

        });

        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> sexCount = userDTOService.selectSexCount();
                map.put("sexCount",sexCount);
                countDownLatch.countDown();
            }

        });
        countDownLatch.await();
        return map;

    }

}
